package com.fuhao.activeMQ.listener;


import com.fuhao.dao.UserMapper;
import com.fuhao.model.Mail;
import com.fuhao.model.User;
import com.fuhao.service.ProductService;
import com.fuhao.service.impl.QueueProductServiceImpl;
import com.fuhao.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import javax.jms.*;
import java.io.Serializable;

@Component
public class QueueListener1 implements SessionAwareMessageListener<Message> {

////    @Transactional
//    @Override
//    public void onMessage(Message message) {
//        System.out.println("进来消费了");
//        //int i = 1/0;//测试重试机制是否配置成功
//        ObjectMessage objectMessage = (ObjectMessage) message;
//        try {
//            Mail mail = (Mail) objectMessage.getObject();
//            System.out.println("queue:消费者1收到的信息：" +mail);
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//    }

//    @Override
//    public void onMessage(Message message) {
//        System.out.println("进来消费了");
//        ObjectMessage objectMessage = (ObjectMessage) message;
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        //int i = 1/0;//测试重试机制是否配置成功
//        try {
//            User user = (User) objectMessage.getObject();
//            ProductService queue = (ProductService) utils.getBean("queue");
//            queue.saveUser(user);
//           // message.acknowledge();
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//    }
    @Transactional
    @Override
    public void onMessage(Message message, Session session) throws JMSException {
        System.out.println("进来消费了");
        ObjectMessage objectMessage = (ObjectMessage) message;
        //
        try {
            User user = (User) objectMessage.getObject();
            ProductService queue = (ProductService) utils.getBean("queue");
            queue.saveUser(user);
            int i = 1/0;//测试重试机制是否配置成功
            message.acknowledge();
        } catch (Exception e) {
            //session.rollback();
            //session.recover();
            e.printStackTrace();
        }
    }
}
